﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Facade;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;


namespace IronOne.SalesOne.SalesOneWeb.ProductLibrary
{
    public partial class SearchManageBenefit : SOSecurityPage, IDataPage, IntTest
    {
        private int pageNo = 1;
        List<QuoLifeBenefit> benefits;
        FinanceFacade benefitFacade = new FinanceFacade();

        protected void Page_Load(object sender, EventArgs e)
        {
            String benefitUpdateCode = string.Empty;
            String benefitViewCode = string.Empty;
            //lblMsg.Text = "";
            btnSearch.Visible = false;




            if (!IsPostBack)
            {
                pageNo = 1;
                UCGrid1.SetText("1");
                FillDropdownBenefit("BenefitCode");
                UCGridButtons1.SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));
            }


            if (Session["successCodeUpdate"] != null)
            {
                MassageSummery("IIS402");
                Session.Remove("successCodeUpdate");
            }

            if (Session["failCodeUpdate"] != null)
            {
                MassageSummery("IIS502");
                Session.Remove("failCodeUpdate");
            }

            if (!Page.IsPostBack)
            {
                pageNo = 1;
                UCGrid1.SetText("1");

                if (Request.QueryString["update"] != null)
                {
                    //pageNo = 1;
                    //UCGrid1.SetText("1");
                    benefitUpdateCode = Request.QueryString["update"];
                    SearchBenefits(benefitUpdateCode, 1);
                    ddlBenefitCode.SelectedValue = benefitUpdateCode;

                }
                if (Request.QueryString["view"] != null)
                {
                    //pageNo = 1;
                    //UCGrid1.SetText("1");
                    benefitViewCode = Request.QueryString["tt"];
                    SearchBenefits(benefitViewCode, 1);
                    ddlBenefitCode.SelectedValue = benefitViewCode;
                }
            }
            else
            {

            }

            if (!IsPostBack && (Request.QueryString["update"] != null))
            {
                this.UCGridButtons1.Visible = true;
                return;
            }

            if (!IsPostBack && (Request.QueryString["view"] != null))
            {
                this.UCGridButtons1.Visible = true;
                return;
            }

            else if (!IsPostBack)
            {
                this.UCGridButtons1.Visible = true;
            }
            else
            {
                this.UCGridButtons1.Visible = true;
            }
            try
            {
                if (benefits.Count == 0)
                {
                    UCGridButtons1.Visible = false;
                    UCGrid1.Visible = false;
                }
            }
            catch (Exception ex) { }


        }

        #region IDataPage Members

        public void View()
        {

            if (Session["benefitCode"] == null)
            {

                Session["benefitCode"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
                Session.Remove("firstRow");
            }

            if (Session["benefitCode"] != null)
            {
                Response.Redirect("~/ProductLibrary/ManageBenefits.aspx?a=v&view= " + ddlBenefitCode.Text, false);
            }
            else
            {
                MassageSummery("IIS503");
                Session["benefitCode"] = null;
            }
        }

        public void Update()
        {
            if (Session["benefitCode"] == null)
            {

                Session["benefitCode"] = ((GridViewRow)Session["firstRow"]).Cells[2].Text;
                Session.Remove("firstRow");
            }

            if (Session["benefitCode"] != null)
            {
                Response.Redirect("~/ProductLibrary/ManageBenefits.aspx?a=u&update= " + ddlBenefitCode.Text, false);
            }
            else
            {
                MassageSummery("IIS503");
                Session["benefitCode"] = null;
            }
        }

        public void SearchBenefits(string benecode, int page)
        {
            benefitFacade = new FinanceFacade();
            int[] hideColumn = { 1, 3, 9 };
            String benefitName = ddlBenefitCode.SelectedValue;


            if (benefitName != "Select...")
            {
                benefits = benefitFacade.GetBenefit(benefitName, page);
                if (benefits.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                {
                    UCGrid1.IsLastPage = (benefitFacade.GetBenefit(benefitName, page + 1)).Count > 0 ? false : true;
                }

                if (benefits.Count == 0)
                {
                    UCGridButtons1.Visible = false;
                    UCGrid1.Visible = false;
                    MassageSummery("IIS504");
                }
                else
                {
                    UCGrid1.Visible = true;

                    UCGrid1.styleList = new List<GridViewStyle> {

                
                new GridViewStyle{
                    ColumnId = "BenefitCode",
                    ColumnHeader = "Benefit Code",
                    ColumnWidth = 100},
                new GridViewStyle{
                    ColumnId = "BenefitExpireStatus",
                    ColumnHeader = "Benefit Expire Status",
                    ColumnWidth = 130},
                new GridViewStyle{
                    ColumnId = "PdSt",
                    ColumnHeader = "Pd St",
                    ColumnWidth = 80},
                new GridViewStyle{
                    ColumnId = "PmSt",
                    ColumnHeader = "Pm St",
                    ColumnWidth = 80},
                 new GridViewStyle{
                    ColumnId = "IbrvSt",
                    ColumnHeader = "Ibrv St",
                    ColumnWidth = 80},
                 new GridViewStyle{
                    ColumnId = "DefendentGrp",
                    ColumnHeader = "Dependent Grp",
                    ColumnWidth = 120}
                    };

                    UCGrid1.SetDataSource(benefits, hideColumn);
                }
            }
            else
            {

                benefits = benefitFacade.GetBenefits(benefitName, page);
                if (benefits.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                {
                    UCGrid1.IsLastPage = (benefitFacade.GetBenefits(benefitName, page + 1)).Count > 0 ? false : true;
                }

                if (benefits.Count == 0)
                {
                    UCGridButtons1.Visible = false;
                    UCGrid1.Visible = false;
                    MassageSummery("IIS504");
                }
                else
                {
                    UCGrid1.Visible = true;

                    UCGrid1.styleList = new List<GridViewStyle> {

               
                new GridViewStyle{
                    ColumnId = "BenefitCode",
                    ColumnHeader = "Benefit Code",
                    ColumnWidth = 100},
                new GridViewStyle{
                    ColumnId = "BenefitExpireStatus",
                    ColumnHeader = "Benefit Expire Status",
                    ColumnWidth = 130},
                new GridViewStyle{
                    ColumnId = "PdSt",
                    ColumnHeader = "Pd St",
                    ColumnWidth = 80},
                new GridViewStyle{
                    ColumnId = "PmSt",
                    ColumnHeader = "Pm St",
                    ColumnWidth = 80},
                 new GridViewStyle{
                    ColumnId = "IbrvSt",
                    ColumnHeader = "Ibrv St",
                    ColumnWidth = 80},
                 new GridViewStyle{
                    ColumnId = "DefendentGrp",
                    ColumnHeader = "Dependent Grp",
                    ColumnWidth = 120}
                    };

                    UCGrid1.SetDataSource(benefits, hideColumn);
                }
            }
        }
        public void Cancel()
        {

        }

        public void Delete()
        {

        }

        public void Create()
        {

        }

        #endregion

        public void FillDropdownBenefit(string type)
        {
            List<QuoLifeBenefit> qlb = benefitFacade.GetBenefitsD("BenefitName ASC");
            QuoLifeBenefit benefit = new QuoLifeBenefit();
            benefit.BenefitCode = "Select...";
            qlb.Insert(0, benefit);
            ddlBenefitCode.DataSource = qlb;
            ddlBenefitCode.DataTextField = "BenefitCode";
            ddlBenefitCode.DataValueField = "BenefitCode";
            ddlBenefitCode.DataBind();

            SearchBenefits("Select...", pageNo);
        }

        protected void ddlBenefitCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            UCGrid1.SetText("1");
            pageNo = 1;
            String benefitType = ddlBenefitCode.Text;
            SearchBenefits(benefitType, 1);
            UCGrid1.SetText("1");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        #region IDataPage Members - btn session

        public void GetId(GridViewRow row)
        {
            Session["benefitCode"] = row.Cells[2].Text;
        }

        #endregion

        #region IDataPage Members - Paging


        public void Next()
        {
            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) + 1;
                UCGrid1.SetText(pageNo.ToString());

            }
            else
            {
                pageNo = 2;
                UCGrid1.SetText("2");

            }

            SearchBenefits(string.Empty, pageNo);
        }

        public void Previous()
        {
            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                UCGrid1.SetText(pageNo.ToString());

                SearchBenefits(string.Empty, pageNo);
            }
        }

        public void Go()
        {
            //try
            //{

            int p = int.Parse(UCGrid1.GetText());
            if (p != 0)
            {
                try
                {
                    SearchBenefits("test", p);
                    pageNo = p;
                }
                catch (Exception)
                {

                    throw;
                }


            }
            else
            {
                // MassageSummery("IIS502");
            }


            //}
            //catch (Exception ex)
            //{
            //    MassageSummery("IIS502");
            //}
        }

        #endregion



    }
}
